#include <ximera/xml/xmlnode.hpp>

#if defined(XIMERA_PRAGMA_COMMENT_LIB) && !defined(XIMERA_NO_AUTOLINK)

#if defined(XIMERA_DEBUG)
    #pragma comment(lib, "tinyxmld.lib")
#else
    #pragma comment(lib, "tinyxml.lib")
#endif

#endif

namespace ximera { namespace xml
{
    XmlNode::XmlNode()
        : _parent(0),
          _prev(0),
          _next(0)
    {
    }

    XmlNode::~XmlNode()
    {
    }

    String const& XmlNode::value() const
    {
        return _value;
    }

    void XmlNode::value(String const& value)
    {
        _value = value;
    }

    bool XmlNode::hasChildren() const
    {
        return !_children.empty();
    }

    void XmlNode::insert(XmlNode* node)
    {        
        if (node->parent())
            XIMERA_THROW(XmlException, L"Node belongs to another parent! Node value: " + node->value());

        node->_parent = this;
        node->_prev = lastChild();
        node->_next = 0;

        _children.push_back(SharedPtr<XmlNode>(node));
    }

    void XmlNode::remove(XmlNode* node)
    {
    }

    void XmlNode::removeAll()
    {
        _children.clear();
    }

    XmlNode* XmlNode::parent() const
    {
        return _parent;
    }

    XmlNode* XmlNode::firstChild() const
    {
        return hasChildren() ? _children.front().get() : 0;
    }

    XmlNode* XmlNode::firstChild(String const& value) const
    {
        return 0;
    }
    
    XmlNode* XmlNode::lastChild() const
    {
        return hasChildren() ? _children.back().get() : 0;
    }

    XmlNode* XmlNode::lastChild(String const& value) const
    {
        return 0;
    }

    XmlNode* XmlNode::previousSibling() const
    {
        return _prev;
    }

    XmlNode* XmlNode::nextSibling() const
    {
        return _next;
    }
}}